package com.ry.novel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ry.novel.bean.UserHolder;
import com.ry.novel.exception.BusinessException;
import com.ry.novel.mapper.UserBookShelfMapper;
import com.ry.novel.model.dto.BookInfo;
import com.ry.novel.model.dto.UserBookShelfInfo;
import com.ry.novel.model.entity.UserBookShelf;
import com.ry.novel.service.BookService;
import com.ry.novel.service.UserBookShelfService;
import com.ry.novel.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ry
 * @since 2022-02-21 10:12
 **/
@Slf4j
@Service
public class UserBookShelfServiceImpl implements UserBookShelfService {

    @Autowired
    private UserBookShelfMapper userBookShelfMapper;

    @Autowired
    private BookService bookService;

    /**
     * 查询当前用户的书架记录
     *
     * @return
     */
    @Override
    public List<UserBookShelfInfo> queryCurrentUser() {
        QueryWrapper<UserBookShelf> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", UserHolder.userInfo.get().getId());

        List<UserBookShelf> shelfList = userBookShelfMapper.selectList(wrapper);
        List<UserBookShelfInfo> infoList = shelfList.stream().map(entity -> {
            UserBookShelfInfo userBookShelfInfo = new UserBookShelfInfo();
            userBookShelfInfo.setId(entity.getId());
            userBookShelfInfo.setUserId(entity.getUserId());
            userBookShelfInfo.setBookId(entity.getBookId());
            userBookShelfInfo.setPreContentId(entity.getPreContentId());
            userBookShelfInfo.setCreateTime(DateUtil.format(entity.getCreateTime()));
            userBookShelfInfo.setUpdateTime(DateUtil.format(entity.getUpdateTime()));
            BookInfo book = bookService.queryById(userBookShelfInfo.getBookId());
            userBookShelfInfo.setBookName(book.getBookName());
            userBookShelfInfo.setLastIndexName(book.getLastIndexName());
            userBookShelfInfo.setPicUrl(book.getPicUrl());
            userBookShelfInfo.setAuthorName(book.getAuthorName());

            return userBookShelfInfo;
        }).collect(Collectors.toList());

        log.info("infos"+infoList);
        return infoList;
    }

    /**
     * 添加到书架
     *
     * @param userBookShelfInfo
     */
    @Override
    public void addBookShelf(UserBookShelfInfo userBookShelfInfo) {
        if (queryBookInShelf(userBookShelfInfo.getBookId())) {
            throw new BusinessException("该小说已经加入到书架了");
        }
        UserBookShelf userBookShelf = new UserBookShelf();
        userBookShelf.setUserId(UserHolder.userInfo.get().getId());
        userBookShelf.setBookId(userBookShelfInfo.getBookId());
        //冗余字段
        userBookShelf.setPreContentId(null);
        userBookShelf.setCreateTime(LocalDateTime.now().toString());
        userBookShelf.setUpdateTime(LocalDateTime.now().toString());

        userBookShelfMapper.insert(userBookShelf);
    }

    @Override
    public Boolean queryBookInShelf(Long bookId) {
        QueryWrapper<UserBookShelf> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", UserHolder.userInfo.get().getId());
        wrapper.eq("book_id", bookId);

        UserBookShelf userBookShelf = userBookShelfMapper.selectOne(wrapper);

        //不为空就是存在书架中
        return userBookShelf != null;
    }

    @Override
    public void deleteBookShelf(Long id) {
        userBookShelfMapper.deleteById(id);
    }
}
